package user

import (
	"context"
	"im_sys/apps/user/rpc/userclient"
	"im_sys/pkg/ctxdata"

	"im_sys/apps/user/api/internal/svc"
	"im_sys/apps/user/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type DetailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 获取用户信息
func NewDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DetailLogic {
	return &DetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DetailLogic) Detail(req *types.UserInfoReq) (resp *types.UserInfoResp, err error) {
	// todo: add your logic here and delete this line
	//1. 从ctx上下文中获取jwt解析后得到的uid
	uid := ctxdata.GetUid(l.ctx)
	infoResp, err := l.svcCtx.User.GetUserInfo(l.ctx, &userclient.GetUserInfoReq{Id: uid})
	if err != nil {
		return nil, err
	}

	return &types.UserInfoResp{
		Info: &types.User{
			Id:       infoResp.User.Id,
			Nickname: infoResp.User.Nickname,
			Phone:    infoResp.User.Phone,
			Sex:      infoResp.User.Sex,
			Avatar:   infoResp.User.Avatar,
		},
	}, nil
}
